const app = getApp()
import {
  listUserAddress,
  deleteDeliveryAddressById,
  setDefaultAddress
} from "../../service/delivery-address"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否需要产品注册证
    needQualifications: false,
    // 是否展示运费规则
    isShowShippingFeeRules: false,
    // 用户收货地址列表
    deliveryAddressList: [],
    // 是否展示地址选择Popup
    showDeliveryAddressPopup: false,
    // 是否展示确认删除收货地址对话框
    isShowDeliveryAddressDeleteDialog: false,
    // 是否展示商品清单
    showProductList: false,
    // 是否展示优惠详情
    showDiscountDetails: false
  },
  // 点击展示商品列表
  onTapShowProductList() {
    this.setData({
      showProductList: true
    })
  },
  // 点击展示优惠详情
  onTapShowDiscountDetails() {
    this.setData({
      showDiscountDetails: true
    })
  },
  /**
   * 点击前往编辑商品页面
   */
  onTapEditAddressButton(event) {
    const addressId = event.currentTarget.dataset.addressId
    const self = this
    wx.navigateTo({
      url: `/pages/UpdateDeliveryAddress/UpdateDeliveryAddress?addressId=${addressId}`,
      events: {
        update() {
          self.loadDeliveryAddressList()
        }
      }
    })
  },
  /**
   *  点击更改收货地址
   */
  onTapChangeReceiveInfo(event) {
    const addressId = event.currentTarget.dataset.addressId
    const address = this.data.deliveryAddressList.find(item => item.id === addressId)
    console.log(address);
    this.setData({
      receiverProvince: address.province,
      receiverCity: address.city,
      receiverDistrict: address.district,
      receiverName: address.receiverName,
      receiverPhone: address.receiverPhone,
      receiverAddress: address.detailAddress,
      showDeliveryAddressPopup: false
    })


  },

  /**
   * 点击设置默认收货地址
   */
  async onTapSetDefaultDeliveryAddress(event) {
    const addressId = event.currentTarget.dataset.addressId
    await setDefaultAddress(addressId)
    this.loadDeliveryAddressList()
  },
  // 清空Promise回调
  clearCallbacks() {
    this.setData({
      resolveCallback: null,
      rejectCallback: null
    });
  },
  /**
   * 展示对话框 
   */
  showConfirmDeleteAddressDialog() {
    this.clearCallbacks()
    return new Promise((resolve, reject) => {
      this.setData({
        isShowDeliveryAddressDeleteDialog: true,
        resolveCallback: resolve,
        rejectCallback: reject
      })
    })
  },

  /**
   *  点击删除收货地址
   */
  async onTapDeleteAddressButton(event) {
    const addressId = event.currentTarget.dataset.addressId
    try {
      await this.showConfirmDeleteAddressDialog()
      await deleteDeliveryAddressById(addressId)
      this.loadDeliveryAddressList()
    } catch {
      return
    }
  },
  /**
   * 点击取消删除收货地址按钮
   */
  onTapCancelDeleteDeliveryAddressButton() {
    this.setData({
      isShowDeliveryAddressDeleteDialog: false
    })
    this.data.rejectCallback(false);
    this.clearCallbacks()
  },
  /**
   * 点击确认删除收货地址按钮
   */
  onTapConfirmDeleteDeliveryAddressButton() {
    this.setData({
      isShowDeliveryAddressDeleteDialog: false
    })
    this.data.resolveCallback(true);
    this.clearCallbacks()
  },
  /**
   * 点击支付按钮
   */
  onTapSubmitButton(){

  },
  /**
   * 点击查看优惠券
   */
  onTapCheckCoupon(){
    wx.showToast({
      title: '暂无优惠券可用',
      icon:'none'
    })
  },
  /**
   * 关闭所有的Popup
   */
  onClosePopup() {
    this.setData({
      showDeliveryAddressPopup: false,
      showProductList: false,
      showDiscountDetails: false
    })
  },
  /**
   * 点击选择用户收货地址
   */
  onTapSelectUserDeliveryAddress() {
    this.setData({
      showDeliveryAddressPopup: true
    })
  },
  /**
   * 加载收货地址
   */
  async loadDeliveryAddressList() {
    const self = this
    // 请求所有的收货地址
    listUserAddress().then(res => {
      console.log("请求收货地址", res.data);
      self.setData({
        deliveryAddressList: res.data
      })
    })
  },


  /**
   * 点击新增收货地址按钮
   */
  onTapAddDeliveryAddressButton() {
    this.onClosePopup()
    const self = this
    wx.navigateTo({
      url: '/pages/DeliveryAddress/AddDeliveryAddress/AddDeliveryAddress',
      events: {
        acceptDeliveryAddressInfo(data) {
          self.setData({
            receiverProvince: data.province,
            receiverCity: data.city,
            receiverDistrict: data.district,
            receiverAddress: data.detailAddress,
            receiverName: data.receiverName,
            receiverPhone: data.receiverPhone
          })
          self.loadDeliveryAddressList()
        }
      },
      success(res) {
        // 向目标页面发送数据
      }
    })
  },
  /**
   * 点击查看运费规则
   */
  onTapCheckShippingFeeRules() {
    this.setData({
      isShowShippingFeeRules: true
    })
  },
  /**
   * 点击关闭运费弹窗
   */
  onTapHaveReadShippingRulesButton() {
    this.setData({
      isShowShippingFeeRules: false
    })
  },
  /**
   * 点击更改是否需要产品资质按钮
   */
  onChangeNeedQualifications({
    detail
  }) {
    this.setData({
      needQualifications: detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const self = this
    this.setData({
      ...app.globalData.orderInfo
    })

    this.loadDeliveryAddressList()
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})